import {
	getCookie,
	getStore,
	setStore,
	removeStore
} from '../utils/mUtils'
let baseUrl = '/api'

function apiReq(url, system, data = {}, header={pagekey:''}) {
	let sysFlag = 'wx'
	try{
		if(sdAndroid!=undefined){
			sysFlag='android'
			if(sdAndroid.checkVersion()=='NO'){
				uni.showToast({
					title: '请求发生错误！当前版本已失效！请联系销售方下载新版本！',
					icon: 'none',
					duration: 3000
				})
				return
			}
		}
	}catch (e){
		console.log(e)
	}
	let reqUrl = baseUrl + system + "/" + url
	let token = null; // 获取token
	let sessionId = null;
	var ua = navigator.userAgent.toLowerCase();//获取判断用的对象

	token=uni.getStorageSync('custoken')
	sessionId = uni.getStorageSync('sessionId')

 	uni.getStorage({
		key: 'custoken',
		success: function(v) {
			token = v.data
			console.log(token)
		}
	}) 
 	uni.getStorage({
			key: 'sessionId',
			success: function(v) {
				sessionId = v.data
			}
		}) 
	if(token==null||token==''){
		token = null
	}

	return uni.request({
		url: reqUrl, //仅为示例，并非真实接口地址。
		data: data,
		method: 'POST',
		header: {
			'appFlag':sysFlag,
			'custoken': token,
			'X-Browser-j': sessionId,
			"pagekey":header.pagekey
		}
	}).then(res => {
		
		if (res[1].statusCode == 200) {
			//如果一切正常
			if (res[1].data.code == 10000) {
				res[1].data._result = true
				return res[1].data
			} else {
				res[1].data._result = false
			}
			let front = (res[1].data.code + "").substr(0, 1);
			//如果是提示消息
			if (front == 3) {
				uni.showToast({
					title: res[1].data.msg,
					icon: 'none',
					duration: 3000
				})
				 throw res[1].data
				// return res[1].data
			}
			//其他处理
			if (front == 2) {
				// todo 特殊处理
				// 如果是未登录
				if (res[1].data.code == 200007) { 
						//localStorage.clear();
										/* window.location.href = "/api/single-sign-on-center/customer/acquireUserWxLogin/white"
										return */
					//if (ua.match(/MicroMessenger/i) == "micromessenger") {
						//在微信中打开
						window.location.href = "/api/single-sign-on-center/customer/acquireUserWxLogin/white"
					//}
			
					return
				}
					if (res[1].data.code == 200017) { 
						uni.showToast({
							title: JSON.stringify(res[1].data),
							icon: 'none',
							duration: 3000
						})
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}
				if (res[1].data.code == 200000) {
					//如果一切正常
					console.log('200000')
					return res[1].data
					// uni.showToast({
					// 	title: "未知错误！请刷新页面重试！",
					// 	icon: 'none',
					// 	duration: 3000
					// })
				}
				if(res[1].data.code==222222){
					location.href=res[1].data.msg+"&_redir="+window.btoa(window.location.href)
					location.href=res[1].data.msg+"&_redir="+window.btoa(window.location.href)
				}
				/* if(res[1].data.code == 200109){
					//跳转到绑定手机号页面
					uni.navigateTo({
						url: '/pages/login/login?status=register'
					})
				} */
				 throw res[1].data
			}
		}
		else if(res[1].statusCode == 403){
			if (res[1].data.code == 200007) {
				localStorage.clear();
				window.location.href = "/api/single-sign-on-center/customer/acquireUserWxLogin/white"
			/* 	if (ua.match(/MicroMessenger/i) == "micromessenger") {
					//在微信中打开
					window.location.href = "/api/single-sign-on-center/customer/acquireUserWxLogin/white"
				}
				else{
					uni.navigateTo({
						url: '/pages/login/login'
					})
				} */
				return
			}
			 uni.navigateTo({
				url: '/pages/tabbar/other/disabled'
			}); 
			return
		}
	}).catch(parmas => {
		switch (parmas.statusCode) {
			case 401:
				break;
			default:
				throw parmas
				break
		}
		//}
	})

}

let api="/api"

let local = {
	userData(url, data) {
		return apiReq(url, '/user-data-system', data)
	},
	userRecharge(url, data) {
		return apiReq(url, '/user-recharge-system', data)
	},
	singleSinOn(url, data) {
		return apiReq(url, '/single-sign-on-center', data)
	},
	sendCheck(url, data) {
		return apiReq(url, '/oceanside-send-and-check', data)
	},
	crm(url, data) {
		return apiReq(url, '/crm-system', data)
	},
	singOn(url, data){
		return apiReq(url, '/single-sign-on-center', data)
	},
	OEMSetting(url, data){
		return apiReq(url, '/OEM-setting-system', data)
	},
}

export default {
	install(vm) {
		vm.prototype.$req = local;
	}
}
